/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **   Swap the Master and Slave role for current deployment.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.GRMConfigManager;
import com.hp.snap.gr.utils.SSConfigUtils;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 
 * Swap the MASTER_SLAVE value pair in the global configuration file and then import the updated global configuration.
 * 
 * There is no parameter configured in the action.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen zhenhua
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class SwapMasterSlaveRoleActionHandler extends AbstractActionHandler {
    private static Logger logger = LoggerFactory.getLogger(SwapMasterSlaveRoleActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {
        if (logger.isDebugEnabled()) {
            logger.debug("Starting to SWAP Master-Slave role...");
        }
        try {
            String ssConfigFile = GRMConfigManager.getGRGlobalConfigPath();
            SSConfigUtils.readSSConfig(ssConfigFile);

            boolean result;
            result = Utils.swapMasterSlaveRole(Constants.CFG_GRG_MASTER_SLAVE_RELATION, Constants.CFG_GRG_ROOT);

            if (result) {
                SSConfigUtils.saveSSConfig(ssConfigFile);

                SSConfigUtils.importConfigServer(ssConfigFile);
            }

            if (logger.isDebugEnabled()) {
                logger.debug("SWAP Master-Slave role finished!");
            }
        } catch (ActionException e) {
            throw e;
        } catch (Exception e) {
            throw new ActionException(e.getMessage(), e);
        }

    }

//	 private boolean swapMasterSlaveRole(String masterSlave, String grgRoot) throws ActionException {
//
//	        String oldMasterSlave = Utils.getConfigurationValue(grgRoot, masterSlave);
//	        if(!StringUtils.isEmpty(oldMasterSlave)){
//	        	String newMasterSlave = swapRole(oldMasterSlave);
//	        	if(logger.isDebugEnabled()){
//	        		logger.debug("the old master_slave is:{} new master_slave after swaping roles is:{}", new Object[]{oldMasterSlave,newMasterSlave});
//	        	}
//	        	SSConfigUtils.replaceConfig(Constants.CFG_GRG_ROOT, Constants.CFG_GRG_MASTER_SLAVE_RELATION, oldMasterSlave, newMasterSlave);
//	        }else{
//	        	logger.error("The Master_Slave configuration does not exist!!");
//	        	throw new ActionException("The Master_Slave configuration does not exist!!");
//	        }
//	        return true;
//	    }
//	 
//	 
//	 private String swapRole(String masterSlaveStr){
//		 StringBuffer strBuf = new StringBuffer();
//			String str = "A1:B1|A2:B2|A3:B3";
//			String[] ss1 = str.split("\\|");
//			for(String s1:ss1){
//				String[] ss2 = s1.split(":");
//				strBuf.append(ss2[1]).append(":").append(ss2[0]).append("|");
//			}
//			
//			return strBuf.toString().substring(0, strBuf.length()-1);
//	 }

}
